package com.zoke.daycarpool.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeAddress;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.zoke.daycarpool.R;
import com.zoke.daycarpool.adapter.SearchAdapter;
import com.zoke.daycarpool.base.BaseUi;
import com.zoke.daycarpool.domain.Address;
import com.zoke.support.image.ShowImagesAdapter.AdaterListener;
import com.zoke.support.view.XListView;
import com.zoke.support.view.ZokeToast;

/**
 * 搜索地址
 * 
 * @author jack
 * 
 */
public class LocationSearch extends BaseUi implements OnGeocodeSearchListener,
		AdaterListener {
	private XListView mListView;
	private SearchAdapter mAdapter;
	private List<Address> mList = new ArrayList<Address>();
	private View mBottomView;

	private GeocodeSearch mGeoSearch;

	public static final String CITYCODE = "_cityCode";

	private String mCityCode;// 城市code

	public static final int RESULTCODE_ADDRESS = 999;//

	public static final String ADDRESS = "_address";
	// 经纬度
	public static final String ADD_LAT = "_add_lat";
	public static final String ADD_LNG = "_add_lng";
	private EditText mSEt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_searchlocation);
		mTitleManager.setLeftIcon(R.drawable.title_back);
		mEnableSwipeRight = true;
		mGeoSearch = new GeocodeSearch(this);
		mGeoSearch.setOnGeocodeSearchListener(this);
		mBottomView = LayoutInflater.from(getApplicationContext()).inflate(
				R.layout.include_search_bottom, null);
		mCityCode = getIntent().getStringExtra(CITYCODE);
		findView();
		mListView.addFooterView(mBottomView);
		doEvent();
		mAdapter = new SearchAdapter(LocationSearch.this, mList, this);
		mListView.setAdapter(mAdapter);
	}

	/** 根据名字搜素地址 **/
	private void searchAddressByName(String name, String citycode) {
		if (mGeoSearch == null)
			return;
		GeocodeQuery query = new GeocodeQuery(name, citycode);
		mGeoSearch.getFromLocationNameAsyn(query);
	}

	@Override
	protected void findView() {
		mListView = (XListView) findViewById(R.id._listView);
		mSEt = (EditText) findViewById(R.id._title_et);
	}

	@Override
	protected void doEvent() {
		mListView.setPullLoadEnable(false);
		mListView.setPullRefreshEnable(false);
		mSEt.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				// 执行搜索
				mList.clear();
				searchAddressByName(s.toString(), "010");
			}
		});
	}

	@Override
	public void onGeocodeSearched(GeocodeResult result, int rCode) {
		if (rCode == 0) {
			if (result != null && result.getGeocodeAddressList() != null
					&& result.getGeocodeAddressList().size() > 0) {
				// GeocodeAddress address =
				// result.getGeocodeAddressList().get(0);
				// String addressName = "经纬度值:" + address.getLatLonPoint()
				// + "\n位置描述:" + address.getFormatAddress();
				// ZokeToast.show(getApplicationContext(), "" + addressName);
				for (GeocodeAddress address : result.getGeocodeAddressList()) {
					Address ad = new Address();
					ad.setAddress(address.getFormatAddress());
					ad.setTitle(address.getDistrict());
					LatLonPoint pl = address.getLatLonPoint();
					ad.setLat(pl.getLatitude() + "");
					ad.setLng(pl.getLongitude() + "");
					mList.add(ad);
				}
				mAdapter.notifyDataSetChanged();
			} else {
				// ZokeToast.show(getApplicationContext(), "返回结果没有");
			}
		} else {
			// 32 鉴权失败 key有问题
			/**
			 * 错误码的含义如下所示：
			 * 
			 * 0表示正常
			 * 
			 * 21表示IO 操作异常
			 * 
			 * 22表示连接异常
			 * 
			 * 23表示连接超时
			 * 
			 * 24表示无效的参数
			 * 
			 * 25表示空指针异常
			 * 
			 * 26表示url 异常
			 * 
			 * 27表示未知主机
			 * 
			 * 28表示服务器连接失败
			 * 
			 * 29表示协议解析错误
			 * 
			 * 30表示http 连接失败
			 * 
			 * 31表示未知的错误
			 * 
			 * 32表示key 鉴权失败
			 */
			ZokeToast.show(getApplicationContext(), "rCode==" + rCode);
		}
	}

	@Override
	public void onRegeocodeSearched(RegeocodeResult arg0, int arg1) {

	}

	@Override
	public void onAdapterItemClick(int position, Object obj) {
		Intent data = new Intent();
		Address ad = mList.get(position);
		data.putExtra(ADD_LAT, ad.getLat());
		data.putExtra(ADD_LNG, ad.getLng());
		data.putExtra(ADDRESS, ad.getAddress());
		setResult(RESULTCODE_ADDRESS, data);
		LocationSearch.this.onBackPressed();
	}
}
